package com.tvunetworks.center.user.service.impl;

import com.tvunetworks.center.common.util.MyUtil;
import com.tvunetworks.center.user.mapper.UserMapper;
import com.tvunetworks.center.user.mapper.UserSFAccountRelationMapper;
import com.tvunetworks.center.user.model.UserSFAccountRelation;
import com.tvunetworks.center.user.model.vo.UserIdWithEmail;
import com.tvunetworks.center.user.service.UserSFAccountRelationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: UserSFAccountRelationImpl
 * @author lebronchen
 * @create: 2019-06-17 15:58
 **/
@Service
@Slf4j
public class UserSFAccountRelationServiceImpl extends BaseService<UserSFAccountRelation> implements UserSFAccountRelationService {

    @Autowired
    private UserSFAccountRelationMapper userSFAccountRelationMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserIdWithEmail> listUnbindSFAccountUser(String accountId, String email) {
        email = MyUtil.formatCondition(email);
        List<UserIdWithEmail> allList = userMapper.listUserIdAndEmail(email);
        if (CollectionUtils.isEmpty(allList)) {
            return new ArrayList<>();
        }
        List<UserIdWithEmail> bindedList = userSFAccountRelationMapper.listBindedSFAccountUser(accountId, email);
        if (!CollectionUtils.isEmpty(bindedList)) {
            allList.removeAll(bindedList);
        }
        return allList;
    }

    @Override
    public List<UserIdWithEmail> listBindedSFAccountUser(String accountId, String email) {
        email = MyUtil.formatCondition(email);
        return userSFAccountRelationMapper.listBindedSFAccountUser(accountId, email);
    }

    @Override
    @Transactional
    public int correlateSFAccountUserRelation(String accountId, String addUserIds, String removeUserIds) {
        if (StringUtils.isNotBlank(addUserIds)) {
            String[] userIds = addUserIds.split(",");
            for (String userId : userIds) {

                UserSFAccountRelation record = new UserSFAccountRelation()
                        .setAccountId(accountId)
                        .setUserId(userId);
                List<UserSFAccountRelation> existList = super.selectList(record);
                if (CollectionUtils.isEmpty(existList)) {
                    super.insert(record);
                }
            }
        }
        if (StringUtils.isNotBlank(removeUserIds)) {
            String[] userIds = removeUserIds.split(",");
            for (String userId : userIds) {
                UserSFAccountRelation record = new UserSFAccountRelation()
                        .setAccountId(accountId)
                        .setUserId(userId);
                super.deleteByWhere(record);
            }
        }
        return 0;
    }
}
